﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HRMSys.Model;
using System.Data.SqlClient;
using System.Data;

namespace HRMSys.DAL
{
    public class DepartmentDAL
    {
        /// <summary>
        /// 新增部门
        /// </summary>
        /// <param name="depart"></param>
        /// <returns></returns>
        public int Insert(Department depart) 
        {
            int result = 0;

            string sql = "Insert into T_Department(Id,DepartName,IsEnable) values(newid(),@DepartName,0)";
            SqlParameter departNameParam = new SqlParameter("@DepartName",depart.DepartName);

            result = SqlHelper.ExecuteNonQuery(sql, departNameParam);
            return result;
        }

        /// <summary>
        /// 获得所有部门
        /// </summary>
        /// <param name="ignoreDisabled">是否忽略已经禁用的部门?</param>
        /// <returns></returns>
        public Department[] GetAll(bool ignoreDisabled) 
        {
            string sql = "select * from T_Department";
            if (ignoreDisabled == true) 
            {
                sql += " where IsEnable=1";
            }
            DataTable table = SqlHelper.ExecuteDataTable(sql);
            Department[] departs = new Department[table.Rows.Count];
            for (int i = 0; i < table.Rows.Count; i++) 
            {
                DataRow row = table.Rows[i];
                departs[i] = this.ToDepartment(row);
            }
            return departs;
        }

        public Department GetById(Guid id) 
        {
            string sql = "select * from T_Department where Id=@Id";
            SqlParameter idParam = new SqlParameter("Id", id);

            DataTable table = SqlHelper.ExecuteDataTable(sql, idParam);
            if (table.Rows.Count <= 0) 
            {
                return null;
            }
            else if (table.Rows.Count > 1)
            {
                throw new Exception("用户ID有重复!");
            }
            else 
            {
                DataRow row = table.Rows[0];
                Department depart = this.ToDepartment(row);
                return depart;
            }
        }

        /// <summary>
        /// 根据Id更新部门信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Update(Department depart) 
        {
            string sql = "update T_Department set DepartName=@DepartName,IsEnable=@IsEnable where Id=@Id";
            SqlParameter departNameParam = new SqlParameter("@DepartName", depart.DepartName);
            SqlParameter isEnableParam = new SqlParameter("@IsEnable", depart.IsEnable);
            SqlParameter idParam = new SqlParameter("@Id", depart.Id);
            int result = SqlHelper.ExecuteNonQuery(sql, departNameParam, isEnableParam, idParam);
            return result;
        }

        /// <summary>
        /// 组装Department对象
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        private Department ToDepartment(DataRow row) 
        {
            Department depart = new Department();
            depart.Id = (Guid)row["Id"];
            depart.DepartName = (string)row["DepartName"];
            depart.IsEnable = (bool)row["IsEnable"];
            return depart;
        }
    }
}